package nl.weeaboo.sh.objects;

import java.awt.Rectangle;

import nl.weeaboo.sh.Game;
import nl.weeaboo.sh.danmaku.SpellUtil;
import nl.weeaboo.sh.level.Level;
import nl.weeaboo.sh.level.Tile;
import nl.weeaboo.sh.model.DamageRect;

public class PlayerCollidable implements Collidable {

	private Game game;
	private Player player;
	
	public PlayerCollidable(Game g, Player p) {
		game = g;
		player = p;
	}
	
	//Functions
	public void onCollision(Sprite attacker, Sprite defender, Rectangle hitArea, int dmg, DamageRect dr) {
		if (player == attacker) {
			attacker.onHitConnect(defender, hitArea, dr);				
			if (defender.onHit(attacker, dmg, hitArea, dr)) {
				game.getSoundManager().playSound("impact.wav", 1);

				Level level = (Level)game.getLevel();
				float rot = SpellUtil.angleTo(attacker.x, attacker.y, defender.x, defender.y);
				level.addEffect(new HitFlash(game, level, hitArea.x+hitArea.width/2,
						hitArea.y+hitArea.height/2, SpellUtil.angleToDegrees(rot)));
			} else {
				//TODO: game.playSound("invincibleImpact.wav");
			}
		} else if (player == defender) {
			attacker.onHitConnect(defender, hitArea, dr);				
			defender.onHit(attacker, dmg, hitArea, dr);
		}
	}
	public void onSplashCollision(Sprite attacker, Sprite defender, Rectangle hitArea, int dmg, DamageRect dr) {
		if (player == attacker) {
			//Don't damage normal enemies with splash damage
		} else if (player == defender) {
			defender.onHit(attacker, dmg, hitArea,dr);
		}
	}
	public void onCollision(Sprite attacker, Tile damaged, Rectangle hitArea, int dmg) {
		/*
		Level level = (Level)game.getLevel();
		int tx = hitArea.x+hitArea.width/2;
		int ty = hitArea.y+hitArea.height/2;
		double rot = Math.toDegrees(Math.atan2(tx-attacker.x, ty-attacker.y));
		level.addEffect(new HitFlash(game, level, tx, ty, rot));
		game.playSound("impact.wav");
		*/

		damaged.damage(dmg);
	}
	public void onCollision(Tile attacker, Sprite damaged, Rectangle hitArea, int dmg) {
		damaged.damage(dmg);
	}

	//Getters
	public boolean getCollidesDamage(Sprite s) {
		if (s == player) return false;
		if (s instanceof Shot && ((Shot)s).getOwner() == player) return false;
		if (s instanceof Platform) return false;

		return true;
	}
	public boolean getCollidesSplash(Sprite s) {
		return false;
	}

	public Player getSprite() { return player; }
	
	//Setters
	
}
